﻿@using System.Collections.Specialized;
@functions{
    //默认分页函数

    private string Pagebreak(int page, string urlPrefix, string urlParams = "")
    {
        string rv = "";
        if (page > 1)
        {
            rv = urlPrefix + page + "/";
        }
        else
        {
            rv = urlPrefix;
        }
        rv += urlParams;
        return rv;
    }

    private string GetUrlParams()
    {
        string rv = "";
        NameValueCollection queryStrings = Request.QueryString;
        int count = queryStrings.Count;
        if (count > 0)
        {
            rv = "?";
        }
        foreach (string key in queryStrings)
        {
            if (rv.IndexOf("=") > 0)
            {
                rv += "&";
            }
            rv += key + "=" + HttpUtility.UrlEncode(queryStrings[key]);
        }
        return rv;
    }
}
@{
    PageInfo pageInfo = (PageInfo)Model;
    int pageCount = pageInfo.PageCount;
    int pageSize = pageInfo.PageSize;
    int recordCount = pageInfo.RecordCount;
    int currentPage = pageInfo.CurrentPage;
    int m = 2; //左右两边显示页码数
    string urlParams = GetUrlParams();
    string urlPrefix;
    if (ViewData.ContainsKey("path"))
    {
        urlPrefix = (string)ViewData["path"];
    }
    else
    {
        urlPrefix = Html.ColumnUrl();
    }
}
@if (recordCount > 0)
{


    <div class="text-center padding-20">
        <ul class="pagination">
            @{
                if (currentPage > 1)
                {
                    <li>
                        <a href="@Pagebreak(currentPage-1,urlPrefix,urlParams)">&laquo;</a>
                    </li>
                }
                else
                {
                    <li class="disabled">
                        <a>&laquo;</a>
                    </li>
                }

                int startPage = currentPage - m;
                int lastPage = currentPage + m;
                int cha = 0;
                if (lastPage > pageCount)
                {
                    cha = lastPage - pageCount;
                    startPage = startPage - cha;
                    if (startPage < 1)
                    {
                        startPage = 1;
                    }
                    lastPage = pageCount;
                }
                else if (startPage < 1)
                {
                    cha = 1 - startPage;
                    startPage = startPage + cha;
                    lastPage = lastPage + cha;
                    if (lastPage > pageCount)
                    {
                        lastPage = pageCount;
                    }
                }
                if (startPage > 1)
                {
                    <li><a href="@Pagebreak(1,urlPrefix,urlParams)">1 ...</a></li>
                }
                for (int i = startPage; i <= lastPage; i++)
                {
                    <li@(currentPage==i?" class=active":"")>
                        <a href="@Pagebreak(i,urlPrefix,urlParams)">@i</a></li>
                    }
                    if (lastPage < pageCount)
                    {
                        <li><a href="@Pagebreak(pageCount,urlPrefix,urlParams)" )>... @pageCount</a></li>
                    }
                    if (currentPage < pageCount)
                    {
                        <li>
                            <a href="@Pagebreak(currentPage+1,urlPrefix,urlParams)">&raquo;</a>
                        </li>
                    }
                    else
                    {
                        <li class="disabled">
                            <a>&raquo;</a>
                        </li>
                    }
            }
    </ul>
</div>
}